var _Global_Data = {

    "selected_machine_id" : '',      //选中的机器的id

    "selected_machine_aisles" : [],  //被选中的机器的货道信息

    "machine_infos" : [],  //本地存储的机器信息数组

    "all_products" : {},     //全部商品

    "colors" : [],           //产品颜色

    "current_aisle" : '',        //当前货道

    "product_type" : 'Glass',        //选中的产品的类别

    "selected_product_id" : '',     //选中的产品id

    "selected_color_id" : ''         //选中的颜色id

};

var AisleResource = new Resource( API_LIST.AISLE_SET , Vue.http);
var ProductColorResource = new Resource( API_LIST.AISLE_PRODUCT_COLOR , Vue.http);

!function reviseAisle() {

    var vm = new Vue({

        el : "#dashboard",

        data : _Global_Data,

        created : function(){

            var self = this;

            self.initData()
                .then(function(succeed){

                    if(succeed){
                        //给select2设置初始值
                        $("#machine_select").val(self.selected_machine_id).trigger('change');
                    }

                })

        },

        computed : {

//                    商品类型判断
            productJudge : function productJudge(){

                if ( this.product_type == 'Glass' ||
                    this.product_type == 'Lens' ||
                    this.product_type == 'BeautyContactLens') {
                    return true;
                }else {
                    return false
                }

            }

        },

        methods : {
            //点击选择机器型号
            showType : function showType( e ) {

                if( this.selected_machine_id == e.target.value ) return;

                this.selected_machine_id = e.target.value;

                if( this.selected_machine_id == '' ) return;

                this.aisleRequest(this.selected_machine_id);

            },
            //商品类型选择
            glassTypeSelect : function glassTypeSelect( e ) {

                if( this.product_type == e.target.value ) return;

                this.product_type = e.target.value;

                $('.product_select').val('').trigger('change');

                $('.color_select').val('').trigger('change');

            },
            //点击选择商品
            productSelect : function productSelect( e ) {

                var self = this;

                if( self.selected_product_id == e.target.value ) return;

                self.selected_product_id = e.target.value;

                if( self.product_type == 'NursingSupply' || self.product_type == 'TransparentContactLens' ) return;

                self.getProductColor( self.selected_product_id,self.product_type )
                    .then(function(){

                        self.selected_color_id = self.colors[0].id;

                        $('.color_select').val(self.colors[0].id).trigger('change');

                    });
            },
            //颜色选择
            colorSelect : function colorSelect( e ) {

                if( this.selected_color_id == e.target.value ) return;

                this.selected_color_id = e.target.value;

            },
            //请求货道信息的方法
            aisleRequest : function aisleRequest( id ) {

                App.blockUI('#product_single');

                var self = this;

                AisleResource.edit(id)
                    .then(function(res){

                        if( parseInt(res.data.code) === 0 ){

                            App.unblockUI('#product_single');

                            self.selected_machine_aisles = res.data.aisles;

                        }else{

                            App.unblockUI('#product_single');

                            alert(res.error);

                        }

                    })
                    .catch(function(err){

                        console.log(err);

                        App.unblockUI('#product_single');

                        alert('请求货道信息异常，请重试！');

                    });

            },
            //点击设置货道商品的方法
            reviseProduct : function reviseProduct( id ) {

                var self = this;

                self.filterCurrentAisle(id);

                if( !self.current_aisle.typed_product_id ) {

                    this.product_type = 'Glass';

                    $('.product_type_select').val(this.product_type).trigger('change');

                    $('.product_select').val('').trigger('change');

                    $('.color_select').val('').trigger('change');

                    return;

                }

                self.product_type = self.current_aisle.product_type;

                $('.product_type_select').val(self.current_aisle.product_type).trigger('change');

                self.selected_product_id = self.current_aisle.typed_product_id;

                $('.product_select').val(self.current_aisle.typed_product_id).trigger('change');

                if( !self.current_aisle.color ) return;

                self.getProductColor( self.current_aisle.typed_product_id,self.current_aisle.product_type )
                    .then(function(succeed){

                        if(succeed){

                            $('.color_select').val(self.current_aisle.color.id).trigger('change');

                        }

                    });

            },
            //根据商品拿到该商品对应的所有颜色
            getProductColor : function getProductColor( id , type ) {

                var self = this;

                var params = {

                    product_id : id,

                    product_type : type

                };

                App.blockUI('#attention-form');

                return ProductColorResource.index( params )
                    .then(function(res){

                        if( parseInt(res.data.code) === 0 ){

                            self.colors = res.data.colors;

                            App.unblockUI('#attention-form');

                            return true;

                        }else {

                            App.unblockUI('#attention-form');

                            alert(res.data.error);

                        }

                    })
                    .catch(function(err){

                        console.log(err);

                        App.unblockUI('#attention-form');

                        alert('请求颜色列表异常，请重试！');

                    })

            },
            //过滤出当前货道数据的方法
            filterCurrentAisle : function filterCurrentAisle( id ) {

                var self = this;

                self.selected_machine_aisles.forEach(function(item){

                    item.forEach(function(column){

                        if(column.aisle_id == id) {

                            self.current_aisle = column;

                        }

                    });

                });

            },
            //确认货物补充
            confirmAisle : function confirmAisle() {

                var self = this;

                var post_data = {};

                App.blockUI('#attention-form');

                if( !self.selected_color ) {

                    post_data = {

                        'typed_product_id' : self.selected_product_id,

                        'product_type' : self.product_type,

                        'aisle_id' : self.current_aisle.aisle_id,

                        'color_id' : self.selected_color_id

                    }

                }else {

                    post_data = {

                        'typed_product_id' : self.selected_product_id,

                        'product_type' : self.product_type,

                        'aisle_id' : self.current_aisle.aisle_id

                    }

                }

                AisleResource.update( self.selected_machine_id,post_data )
                            .then(function(res){

                                if( parseInt(res.data.code) === 0 ){

                                    alert('设置成功！');

                                    self.current_aisle.typed_product_id = self.selected_product_id;
                                    self.current_aisle.product_type = self.product_type;

                                    if( self.current_aisle.color ){

                                        self.current_aisle.color.id = self.selected_color_id;

                                    }else {

                                        if( self.product_type == 'Glass' ||
                                            self.product_type == 'BeautyContactLens' ||
                                            self.product_type == 'Lens' ){

                                            self.current_aisle.color = {};

                                            self.current_aisle.color.id = self.selected_color_id;

                                        }

                                    }

                                    var current_product = self.all_products[self.product_type].filter(function(item){

                                        return item.id == self.selected_product_id;

                                    })[0];

                                    self.current_aisle.img = current_product.thumb_img;

                                    $('.close').trigger('click');

                                }else {

                                    alert(res.data.error);

                                }

                                App.unblockUI('#attention-form');

                            })
                            .catch(function(err){

                                console.log(err);

                                App.unblockUI('#attention-form');

                                alert('设置货道异常，请重试！');

                            })

            },
            //获取数据
            initData : function initData() {

                var self = this;

                return AisleResource.create()
                    .then(function( res ){

                        var data = res.data;

                        if( parseInt(data.code) === 0 ){

                            self.all_products = data.all_products;

                            self.machine_infos = data.vendor_machines;

                            self.selected_machine_id = data.vendor_machines[0].id;

                            self.aisleRequest(self.selected_machine_id);

                            return true;

                        }else {

                            alert(res.data.error);

                        }

                    })
                    .catch(function( err ){

                        alert("初始化数据异常，请重试！");

                    })

            }
        }
    })

}();